﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace MetaDesigner
{
    /// <summary>
    /// Interaction logic for SiteColumnPicker.xaml
    /// </summary>
    public partial class SiteColumnPicker : Window
    {
        List<SiteColumn> selectedColsList;

        SiteColumnList siteCols;

        private ContentType ct;

        /// <summary>
        /// Initializes a new instance of the <see cref="SiteColumnPicker"/> class.
        /// </summary>
        /// <param name="ct">The content type to pick site columns for.</param>
        /// <param name="FromDump">Get the content type list from the SPDump_ if set to <c>true</c> [from dump].</param>
        public SiteColumnPicker(ContentType ct, bool FromDump=false)
        {
            this.ct = ct;
            selectedColsList = ct.SCList;
            InitializeComponent();

            if (FromDump)
            {
                siteCols = SaveFile.SPDump_siteCols;
                scLabel.Content = "SharePoint Site Columns: ";
            }
            else
            {
                siteCols = SaveFile.siteCols;
                scLabel.Content = "Project Site Columns: ";
            }

            PopulateAllList();
            PopulateSelectedList();

            

            if (AllColumns.Items.Count == 0)
            {
                Label l = new Label();
                l.Content = "It appears you haven't defined any Site Columns yet.\nGo to the Site Columns tab to create some!";
                ((Grid)AllColumns.Parent).Children.Add(l);
                Grid.SetColumn(l, 0);
                Grid.SetRow(l, 1);
                l.Margin = new Thickness(10);
            }
        }


        private void PopulateAllList()
        {
            while (AllColumns.Items.Count > 0) { AllColumns.Items.RemoveAt(0); }

            foreach (SiteColumn sc in siteCols)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Tag = sc;
                lvi.Content = sc.Name;

                AllColumns.Items.Add(lvi);
            }
        }

        private void PopulateSelectedList()
        {
            while (SelectedCols.Items.Count > 0) { SelectedCols.Items.RemoveAt(0); }

            foreach (SiteColumn sc in selectedColsList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Tag = sc;
                lvi.Content = sc.Name;

                SelectedCols.Items.Add(lvi);
            }
            UpdateButtons();
        }

        private void AllColumns_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListViewItem lvi1 = (ListViewItem)SelectedCols.SelectedItem;
            ListViewItem lvi2 = (ListViewItem)AllColumns.SelectedItem;
            if (lvi1 != null && lvi2 != null) { lvi1.IsSelected = false; }
            Deselect.IsEnabled = false;
            if (lvi2 != null && !selectedColsList.Contains(lvi2.Tag))
            {
                Select.IsEnabled = true;
            }
            else
            {
                Select.IsEnabled = false;
            }
        }

        private void SelectedCols_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListViewItem lvi1 = (ListViewItem)SelectedCols.SelectedItem;
            ListViewItem lvi2 = (ListViewItem)AllColumns.SelectedItem;
            if (lvi1 != null && lvi2 != null) { lvi2.IsSelected = false; }
            Select.IsEnabled = false;
            if (lvi1 != null)
            {
                Deselect.IsEnabled = true;
            }
            else
            {
                Deselect.IsEnabled = false;
            }
        }

        private void UpdateButtons()
        {
            if (selectedColsList.Count > 0)
            {
                DeselectAll.IsEnabled = true;
            }
            else
            {
                DeselectAll.IsEnabled = false;
            }

            bool allAlreadySelected = false;
            foreach(SiteColumn sc in siteCols) { if (!selectedColsList.Contains(sc)) { allAlreadySelected = true; } }
            if (allAlreadySelected)
            {
                SelectAll.IsEnabled = true;
            }
            else
            {
                SelectAll.IsEnabled = false;
            }
        }

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (SiteColumn sc in siteCols)
            {
                if (!selectedColsList.Contains(sc))
                {
                    selectedColsList.Add(sc);
                }
            }
            PopulateSelectedList();
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            ListViewItem lvi = (ListViewItem)AllColumns.SelectedItem;
            if (lvi != null && !selectedColsList.Contains((SiteColumn)lvi.Tag))
            {
                selectedColsList.Add((SiteColumn)lvi.Tag);
            }
            PopulateSelectedList();
        }

        private void Deselect_Click(object sender, RoutedEventArgs e)
        {
            ListViewItem lvi = (ListViewItem)SelectedCols.SelectedItem;
            if (lvi != null && selectedColsList.Contains((SiteColumn)lvi.Tag))
            {
                selectedColsList.Remove((SiteColumn)lvi.Tag);
            }
            PopulateSelectedList();
        }

        private void DeselectAll_Click(object sender, RoutedEventArgs e)
        {
            while (selectedColsList.Count > 0)
            {
                selectedColsList.RemoveAt(0);
            }
            PopulateSelectedList();
        }

        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            ((MainWindow)(this.Owner)).UpdateContentTypeTab(ct);
        }

    }
}
